package b3.model
{
	import b3.config.Config;
	import b3.config.TaskConfig;
	import b3.net.ServerEvent;
	import b3.net.ServerRequest;
	
	public class TaskList extends Model
	{
		public var finishTasks:Object;
		
		public function TaskList()
		{
			super();
		}
		
		public function loadTasks(force:Boolean = false):void
		{
			var serverRequest:ServerRequest = new ServerRequest(ServerEvent.LOAD_TASKS, {userId:PlayerManager.getInstance().myUserId});
			serverRequest.setHandleFunc(handleLoadTask);
			serverRequest.post(force);
		}
		
		public function handleLoadTask(data:Object):void
		{
			finishTasks = new Object();
			for (var taskIdStr:String in data)
			{
				finishTasks[taskIdStr] = true;
			}
			checkAndFinishObjectNumTask();
			var pm:PlayerManager = PlayerManager.getInstance(); 
			pm.me.packageModel.addEventListener(ModelEvent.REFRESH, onPackageRefresh);
			pm.addEventListener(ModelEvent.SWITCH_PLAYER, onVisitFriend);
			dispatchEvent(new ModelEvent(ModelEvent.REFRESH));
		}
		
		private function onPackageRefresh(event:ModelEvent):void
		{
			checkAndFinishObjectNumTask();
		}
		
		private function onVisitFriend(event:ModelEvent):void
		{
			if (event.data != null && event.data['visitFriend'])
			{
				finishTask(TaskConfig.ID_VISIT_FRIEND);
			}
		}
		
		public function checkAndFinishObjectNumTask():void
		{
			for (var taskIdStr:String in Config.taskConfigs)
			{
				if (finishTasks[taskIdStr] == null)
				{
					var taskConfig:TaskConfig = Config.taskConfigs[taskIdStr];
					var me:Player = PlayerManager.getInstance().me;
					if (taskConfig != null && taskConfig.type == TaskConfig.TYPE_OBJECT_NUM)
		      		{
		               	if (me.packageModel.getObjectNum(taskConfig.requireIndex) >= taskConfig.requireNum)
		               		finishTask(parseInt(taskIdStr));
		    	    }
		  		}
			}
		}
		
		public function checkAndFinishBuildTask():void
		{
			for (var taskIdStr:String in Config.taskConfigs)
			{
				if (finishTasks[taskIdStr] == null)
				{
					var taskConfig:TaskConfig = Config.taskConfigs[taskIdStr];
					var me:Player = PlayerManager.getInstance().me;
					if (taskConfig != null && taskConfig.type == TaskConfig.TYPE_BUILDING_NUM)
		      		{
		               	if (me.townModel.getBuildingNum(taskConfig.requireIndex) >= taskConfig.requireNum)
		               	{
		               		finishTask(parseInt(taskIdStr));
		               	}
		        	}
	    	    }
			}
		}
		
		public function finishTask(taskId:int):void
		{
			if (finishTasks[taskId] == null)
			{
				finishTasks[taskId] = true;
				var serverRequest:ServerRequest = new ServerRequest(ServerEvent.FINISH_TASK, {taskId:taskId});
				serverRequest.setHandleFunc(handleFinishTask);
				serverRequest.post(true);
			}
		}
		
		public function handleFinishTask(data:Object):void
		{
			if (data['err'] == 0)
			{
				var taskId:int = data['taskId'];
				finishTasks[taskId] = true;
				var me:Player = PlayerManager.getInstance().me;
				me.packageModel.handleLoadObjects(data['package']);
				var modelEvent:ModelEvent = new ModelEvent(ModelEvent.TASK_FINISH);
				modelEvent.data = data;
				dispatchEvent(modelEvent);
			}
		}
	}
}